package com.cloudcc.mobile.dao.impl;

import android.text.TextUtils;

import com.cloudcc.cloudframe.model.DynamicModel;
import com.cloudcc.cloudframe.net.async.EventRequest;
import com.cloudcc.mobile.AppConstant;
import com.cloudcc.mobile.dao.BaseEngine;
import com.cloudcc.mobile.dao.IBeauEngine;
import com.cloudcc.mobile.entity.beau.BeauInfoEntity;
import com.cloudcc.mobile.entity.beau.BeauInfoTabEntity;
import com.cloudcc.mobile.entity.beau.BeauListEntity;
import com.cloudcc.mobile.entity.beau.BeauListFilterEntity;
import com.cloudcc.mobile.entity.beau.BeauRelationFollowEntity;
import com.cloudcc.mobile.event.BeauEventList;
import com.cloudcc.mobile.event.BeauEventList.BeauInfoEvent;
import com.cloudcc.mobile.event.BeauEventList.BeauInfoEventnew;
import com.cloudcc.mobile.event.BeauEventList.BeauListEvent;
import com.cloudcc.mobile.event.BeauEventList.BeauListFilterEvent;
import com.cloudcc.mobile.event.BeauEventList.DynamicListEvent;
import com.cloudcc.mobile.event.BeauEventList.RelateionFollowListEvent;
import com.cloudcc.mobile.manager.UrlManager;
import com.litesuits.android.log.Log;
import com.loopj.android.http.RequestParams;

import java.util.List;

public class BeanEngineImpl extends BaseEngine implements IBeauEngine {

    @Override
    // public void serachBeauList(String viewId, int pageNum, String sortBy,
    // boolean sortModel, String keyword, String expression,
    // BeauListEvent event) {
    public void serachBeauList(String objectId, String viewId, int pageNum,
                               String sortBy, boolean sortModel, String keyword,
                               String expression, BeauListEvent event) {
        EventRequest<List<BeauListEntity>> eventRequest = new EventRequest<List<BeauListEntity>>() {};
        eventRequest.setEvent(event);
        RequestParams params = new RequestParams();
        addParams(params, "viewId", viewId);
        Log.d("viewId", viewId + "dsf");
        addParams(params, "pageNUM", String.valueOf(pageNum));
        addParams(params, "pageSize", String.valueOf(AppConstant.PAGE_SIZE));
        addParams(params, "sort", sortBy);
        addParams(params, "dir", sortModel ? "ASC" : "DESC");
        addParams(params, "viewId", viewId);
        addParams(params, "expression", expression);
        addParams(params, "keyword", keyword);
        addParams(params, "objectId", objectId);
        sendPost(params, "searchByViewIdNew", eventRequest);
    }

    @Override
    public void serachBeauListBaobiao(String objectId, String viewId, int pageNum,
                               String sortBy, boolean sortModel, String keyword,
                               String expression,int number , BeauListEvent event) {
        EventRequest<List<BeauListEntity>> eventRequest = new EventRequest<List<BeauListEntity>>() {
        };
        eventRequest.setEvent(event);
        RequestParams params = new RequestParams();
        addParams(params, "viewId", viewId);
        Log.d("viewId", viewId + "dsf");
        addParams(params, "pageNUM", String.valueOf(pageNum));
        addParams(params, "pageSize", number+"");
        addParams(params, "sort", "rlastmod");
        addParams(params, "dir", sortModel ? "asc" : "desc");
        addParams(params, "viewId", viewId);
        addParams(params, "expression", expression);
        addParams(params, "keyword", keyword);
        addParams(params, "objectId", objectId);
        sendPost(params, "searchByViewIdNew", eventRequest);
    }

    /**
     * 构建请求参数
     *
     * @param params
     * @param paramName  字段名
     * @param paramValue 字段值
     * @return
     */
    private final void addParams(RequestParams params, String paramName,
                                 String paramValue) {
        if (params == null) {
            params = new RequestParams();
        }
        if (!TextUtils.isEmpty(paramValue)) {
            params.add(paramName, paramValue);
        }
    }

    @Override
    public void getFilter(String objectId, BeauListFilterEvent event) {
        EventRequest<BeauListFilterEntity> eventRequest = new EventRequest<BeauListFilterEntity>() {
        };
        eventRequest.setEvent(event);
        RequestParams params = new RequestParams();
        params.put("objectId", objectId);
        // sendPost(params, "getQueryConditions", eventRequest);
        sendPost(params, "getQueryConditionsNew", eventRequest);
    }

    @Override
    public void getBeauInfo(String id, String objApiName, String data,
                            BeauInfoEvent event) {
        EventRequest<BeauInfoEntity> eventRequest = new EventRequest<BeauInfoEntity>() {
        };
        eventRequest.setEvent(event);
        RequestParams params = new RequestParams();
        params.put("recordId", id);
//        params.put("objectApiName", objApiName);
        params.put("data", data);
        sendPost(params, "getDetailHeader", eventRequest);
    }

    //详情页默认tab界面
    @Override
    public void getBeauTabInfo(String objApiName,String prefix,
                               BeauEventList.BeauInfoTabEvent event) {
        EventRequest<BeauInfoTabEntity> eventRequest = new EventRequest<BeauInfoTabEntity>() {
        };
        eventRequest.setEvent(event);
        RequestParams params = new RequestParams();
        params.put("objectId", objApiName);
        params.put("prefix", prefix);
        sendPost(params, "getTabDisplayInfo", eventRequest);
    }

    @Override
    public void getBeauInfoXG(String id, String objApiName,
                              BeauInfoEventnew event) {
        EventRequest<BeauInfoEntity> eventRequest = new EventRequest<BeauInfoEntity>() {
        };
        eventRequest.setEvent(event);
        RequestParams params = new RequestParams();
        params.put("recordId", id);
//        params.put("objectApiName", objApiName);
        sendPost(params, "getDetailRelatedItems", eventRequest);
    }

    @Override
    public void getBeauRelatDynamic(String id, DynamicListEvent event) {
        EventRequest<List<DynamicModel>> eventRequest = new EventRequest<List<DynamicModel>>() {
        };
        eventRequest.setEvent(event);
        sendGet(UrlManager.MOCK_URL + "beaudynamic.json", eventRequest);
    }

    @Override
    public void getBeauRelatFollows(Object data, RelateionFollowListEvent event) {
        EventRequest<BeauRelationFollowEntity> eventRequest = new EventRequest<BeauRelationFollowEntity>() {
        };
        eventRequest.setEvent(event);
        sendPost(data, eventRequest, "getRelationDataByRelationId", true);
    }



}